package com.myplans.client.view.custom;

import com.google.api.gwt.oauth2.client.Auth;
import com.google.api.gwt.oauth2.client.AuthRequest;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.myplans.client.HistoryTokens;
import com.myplans.client.UserUtil;
import com.myplans.shared.FacebookUtil;

public class SignInDialogBox extends Composite implements HasText {

	private static SignInDialogBoxUiBinder uiBinder = GWT
			.create(SignInDialogBoxUiBinder.class);

	interface SignInDialogBoxUiBinder extends UiBinder<Widget, SignInDialogBox> {
	}

	public SignInDialogBox() {
		initWidget(uiBinder.createAndBindUi(this));
//		dialogBox.center();
		dialogBox.hide();
	}

	@UiField
	Button button;
	@UiField
	DialogBox dialogBox;
	@UiField
	TextBox email;
	@UiField
	PasswordTextBox password;
	@UiField
	Button signInButton;
	@UiField
	Button signInWithFacebook;

	public SignInDialogBox(String firstName) {
		initWidget(uiBinder.createAndBindUi(this));
		button.setText(firstName);
	}

	@UiHandler("button")
	void onClick(ClickEvent e) {
		dialogBox.hide();
	}
	private static final Auth AUTH = Auth.get();
	private static final String FACEBOOK_AUTH_URL = "https://www.facebook.com/dialog/oauth";

	  // All available scopes are listed here:
	  // http://developers.facebook.com/docs/authentication/permissions/
	  // This scope allows the app to access the user's email address.
	  private static final String FACEBOOK_EMAIL_SCOPE = "email";

	  // This scope allows the app to access the user's birthday.
	  private static final String FACEBOOK_BIRTHDAY_SCOPE = "user_birthday";
	@UiHandler("signInWithFacebook")
	void onSignInWithFacebookClick(ClickEvent e) {
//		Location.replace(FacebookUtil.getAuthorizeUrl());
		final AuthRequest req = new AuthRequest(FACEBOOK_AUTH_URL, FacebookUtil.APPLICATION_ID)
        .withScopes(FACEBOOK_EMAIL_SCOPE, FACEBOOK_BIRTHDAY_SCOPE)
        // Facebook expects a comma-delimited list of scopes
        .withScopeDelimiter(",");
    AUTH.login(req, new Callback<String, Throwable>() {
      @Override
      public void onSuccess(String token) {
//        Window.alert("Got an OAuth token:\n" + token + "\n"
//            + "Token expires in " + AUTH.expiresIn(req) + " ms\n");
    	  UserUtil.access_token = token;
    	  dialogBox.hide();
    	  History.newItem(HistoryTokens.SIGNIN_WITH_FACEBOOK.getId());
      }

      @Override
      public void onFailure(Throwable caught) {
        Window.alert("Error:\n" + caught.getMessage());
      }
    });
	}

	public void setText(String text) {
		button.setText(text);
	}

	public String getText() {
		return button.getText();
	}
	
	public void show() {
		dialogBox.center();
	}

	public TextBox getEmail() {
		return email;
	}

	public PasswordTextBox getPassword() {
		return password;
	}

	public Button getSignInButton() {
		return signInButton;
	}

	public DialogBox getDialogBox() {
		return dialogBox;
	}
}
